//
//  SketchUpdateController.h
//  RunSketch
//
//  Created by Brian Hinch on 7/27/08.
//  Copyright 2008 Tellart. All rights reserved.
//

#import <UIKit/UIKit.h>

#define kSketchManualUpdateURLFormatString @"http://%@:8080/sketches/index.php"
#define kSketchAutoUpdateURLFormatString @"http://%@:8080/sketches/index.php?autoUpdate"
#define kSketchCapabilitiesURLFormatString @"http://%@:8080/sketches/caps.php"

@protocol SketchUpdateControllerDelegate;

@interface SketchUpdateController : NSObject  {

    id<SketchUpdateControllerDelegate>  delegate;

    NSURLConnection     *theConnection;
    NSMutableData       *receivedData;
    
    NSURL               *rootURL;
    NSString            *baseURL;
    NSString            *sLiveSketchBasePath;
    NSString            *sUpdatingSketchBasePath;

    BOOL                doingTest;
    BOOL                updating;
    BOOL                downloading;

    long                currentDownloadIndex;
    long                totalNumDownloads;

    NSMutableArray      *downloadRelativePathsQue;

    NSString            *lastElementName;
    int                 sketchBytesTotal;
    int                 sketchBytesLoaded;
}

@property (nonatomic, assign) id <SketchUpdateControllerDelegate> delegate;
@property (nonatomic, readonly) BOOL doingTest;
@property (nonatomic, readonly) BOOL updating;
@property (nonatomic, readonly) BOOL downloading;
@property (nonatomic, retain) NSURL *rootURL;

@property (nonatomic, retain) NSString *sLiveSketchBasePath;
@property (nonatomic, retain) NSString *sUpdatingSketchBasePath;
@property (nonatomic, retain) NSString *baseURL;
@property (nonatomic, retain) NSMutableArray *downloadRelativePathsQue;

@property (nonatomic, retain) NSURLConnection *theConnection;
@property (nonatomic, retain) NSMutableData *receivedData;

- (void)updateFromURL:(NSURL *)url;
- (void)testFromURL:(NSURL *)url;
- (void)stop;

-(void) event_didChooseSketch;
-(void) event_didFailWithConnectionError;
-(void) event_didFailWithCommunicationError:(NSString *)msg;
- (void) event_didFailWithCatalogParserError:(NSError *)error;
- (void) event_didLoadCatalog;
-(void) event_madeProgress: (int)bytesLoaded: (int)bytesTotal;
-(void) event_didSucceed;
-(void) event_didStop;

- (void) startDownloads;
- (BOOL)prepareFilesystem;
-(BOOL)replaceLiveSketchWithUpdatingSketch;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)didFinishLoadingFile;
- (void)didFinishLoadingCatalog;
+ (NSString *) getSketchEntryPointPath;
+ (NSURL *) getSketchEntryPointURL;
+ (NSArray *) getSketchingJavaScriptLibraryURLs;
+ (NSString *)reserveRuntimeImagePath:(NSString *)base;
//+ (NSString *)reserveRuntimeSoundPath:(NSString *)base;
//+ (NSString *)reserveRuntimePath:(NSString *)filePathString withBase:(NSString *)base extension:(NSString *)fileExtension;

-(void) event_hostDidFailTest;
-(void) event_hostDidPassTest;

@end



@protocol SketchUpdateControllerDelegate <NSObject>
-(void) didFinishTest: (SketchUpdateController *)c result:(BOOL)passed;
-(void) didChooseSketch: (SketchUpdateController *)c;
-(void) didFailWithConnectionError: (SketchUpdateController *)c;
-(void) didFailWithCommunicationError: (SketchUpdateController *)c message:(NSString *)msg;
-(void) didLoadCatalog: (SketchUpdateController *)c;
-(void) didFailWithCatalogParserError: (SketchUpdateController *)c error:(NSError *)err;
-(void) madeProgress: (SketchUpdateController *)c loaded:(int)bytesLoaded total:(int)bytesTotal;
-(void) didSucceed: (SketchUpdateController *)c;
-(void) didStop: (SketchUpdateController *)c;
@end

